package com.example.beikeyigou.utils;

import android.app.Activity;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;
import java.util.Set;

public class BaseActivity extends AppCompatActivity implements Callback.CommonCallback<String> {
    private String TAG = "xxy_nice";

    public void postNetwork(String url, Map<String, String> map) {
        RequestParams params = new RequestParams(url);
        // 将提交的键值对放到请求参数中
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            params.addParameter(key, map.get(key));
        }
        params.setAsJsonContent(true);
        params.setBodyContentType("application/json;charset=utf-8");

        x.http().post(params, this);
    }

    public void getNetwork(String url) {
        RequestParams params = new RequestParams(url);
        x.http().get(params, this);
    }

    @Override   // 获取数据成功时，调用此方法
    public void onSuccess(String result) {
        Log.i(TAG, "onSuccess: result==" + result);
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {   // 获取数据失败时，调用此方法
        Log.i(TAG, "onError: ex==" + ex.getCause());
        ex.printStackTrace();
        Log.i(TAG, "onError: ex==" + ex.getMessage());
    }

    @Override
    public void onCancelled(CancelledException cex) {   // 获取数据取消时，调用此方法

    }

    @Override
    public void onFinished() {  // 获取数据完成时，调用此方法

    }
}
